#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: tmf_share_cache.py 
@version:
@time: 2022/05/26 
"""
from TapClientCore.T02_tm_api.v2_api.v2_tm import v2tm as TM


class TMFShareCache:

    # 创建
    @staticmethod
    def _post_share_cache(payload):
        return TM.post_share_cache(payload)

    # 删除
    @staticmethod
    def _delete_share_cache_by_id(_id):
        return TM.delete_share_cache_by_id(_id)

    # 查询
    @staticmethod
    def _get_share_cache(_filter):
        return TM.get_share_cache(_filter).get('data')

    def get_share_cache_by_name(self, name):
        limit = 20
        skip = 0
        while True:
            _filter = {
                "order": "createTime DESC",
                "limit": limit,
                "skip": skip,
                "where": {}
            }
            response = self._get_share_cache(_filter)
            items = response.get('items')
            if not items:
                break
            else:
                for one in items:
                    if one.get('name') == name:
                        return one
            skip += limit

    def delete_share_cache_by_name(self, name):
        result = self.get_share_cache_by_name(name)
        if result:
            self._delete_share_cache_by_id(result.get('id'))

    def post_share_cache(self, payload):
        return self._post_share_cache(payload)
